using Engine;
using Engine.Graphics;
using Game;

namespace SCIENEW {
    /// <summary>
    /// 金属锭
    /// </summary>
    public class MetalIngotItem : MeshItem, IMaterialBlock {
        public Materials Type;

        public Materials GetMaterial() => Type;

        bool createOreRecipe;

        public MetalIngotItem(Materials type, bool createOreRecipe = false) : base(type + "Ingot")
        {
            Type = type;
            this.createOreRecipe = createOreRecipe;
            string name = type.ToDisplayName();
            DisplayName = string.Format(LanguageControl.Get("Items", "Names", "MetalIngotItem"), name);
            Description = string.Format(LanguageControl.Get("Items", "Descriptions", "MetalIngotItem"), name);
            m_standaloneBlockMesh.AppendMesh("Models/Ingots", "IronIngot", Matrix.CreateTranslation(0f, -0.1f, 0f), Matrix.Identity, MaterialsUtils.GetColor(type));
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_standaloneBlockMesh, color, 2f * size, ref matrix, environmentData);
        }

        public override string GetCraftingId() => Id;

        public override CraftingRecipe[] GenerateProceduralRecipe(int value) {
            if (Type == Materials.None) return base.GenerateProceduralRecipe(value);
            List<CraftingRecipe> recipes = new();
            if (createOreRecipe) {

                //矿石到锭
                CraftingRecipe chunkToIngot = new CraftingRecipe {
                    Description = $"冶炼{Type.ToDisplayName()}矿石得到{Type.ToDisplayName()}",
                    Ingredients = new string[36],
                    RemainsValue = 0,
                    RemainsCount = 0,
                    RequiredHeatLevel = MaterialsUtils.GetRequiredFuelLevel(Type),
                    RequiredPlayerLevel = 0,
                    ResultValue = ItemsBlockManager.GetItemID(this),
                    ResultCount = 1,
                };
                chunkToIngot.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<OreChunkItem>(Type));
                recipes.Add(chunkToIngot);
                CraftingRecipe chunkToIngot2 = new CraftingRecipe {
                    Description = $"冶炼{Type.ToDisplayName()}矿石得到{Type.ToDisplayName()}",
                    Ingredients = new string[36],
                    RemainsValue = 0,
                    RemainsCount = 0,
                    RequiredHeatLevel = MaterialsUtils.GetRequiredFuelLevel(Type),
                    RequiredPlayerLevel = 0,
                    ResultValue = ItemsBlockManager.GetItemID(this),
                    ResultCount = 4,
                };
                chunkToIngot2.Ingredients[0] = chunkToIngot2.Ingredients[1] = chunkToIngot2.Ingredients[2] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<OreChunkItem>(Type));
                recipes.Add(chunkToIngot2);

                //矿粉到锭
                CraftingRecipe chunkPowderToIngot = new CraftingRecipe {
                    Description = $"冶炼{Type.ToDisplayName()}矿粉得到{Type.ToDisplayName()}",
                    Ingredients = new string[36],
                    RemainsValue = 0,
                    RemainsCount = 0,
                    RequiredHeatLevel = MaterialsUtils.GetRequiredFuelLevel(Type),
                    RequiredPlayerLevel = 0,
                    ResultValue = ItemsBlockManager.GetItemID(this),
                    ResultCount = 1,
                };
                chunkPowderToIngot.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<OrePowderItem>(Type));
                recipes.Add(chunkPowderToIngot);
                CraftingRecipe chunkPowderToIngot2 = new CraftingRecipe {
                    Description = $"冶炼{Type.ToDisplayName()}矿粉得到{Type.ToDisplayName()}",
                    Ingredients = new string[36],
                    RemainsValue = 0,
                    RemainsCount = 0,
                    RequiredHeatLevel = MaterialsUtils.GetRequiredFuelLevel(Type),
                    RequiredPlayerLevel = 0,
                    ResultValue = ItemsBlockManager.GetItemID(this),
                    ResultCount = 4,
                };
                chunkPowderToIngot2.Ingredients[0] = chunkPowderToIngot2.Ingredients[1] = chunkPowderToIngot2.Ingredients[2] = IECraftingRecipesManager.BlockToIngredient(ItemsBlockManager.GetItemIdByMaterial<OrePowderItem>(Type));
                recipes.Add(chunkPowderToIngot2);
            }
            //块到锭
            CraftingRecipe blockToIngot = new CraftingRecipe {
                Description = $"分解{Type.ToDisplayName()}块",
                Ingredients = new string[36],
                RemainsValue = 0,
                RemainsCount = 0,
                RequiredHeatLevel = ComponentExCraftingTable.RequiredHeatLevel,
                RequiredPlayerLevel = 0,
                ResultValue = ItemsBlockManager.GetItemID(this),
                ResultCount = 9,
            };
            blockToIngot.Ingredients[0] = IECraftingRecipesManager.BlockToIngredient(NormalBlockManager.GetNBlockIdByMaterial<MetalNBlock>(Type));
            recipes.Add(blockToIngot);
            return recipes.ToArray();
        }
    }
}